//
//  ios_main.m
//  Prism
//
//  Created by Bas du Pre on 16-05-11.
//  Copyright UU 2011. All rights reserved.
//

#include "global.h"
#if PRISM_TARGET_SDL

#include "../../../Include/ext_libs/sdl/SDL.h"

#include "Prism.h"

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B);
void Slock(SDL_Surface *screen);
void Sulock(SDL_Surface *screen);

bool LockFramesPerSecond(double framerate)
{
    if (framerate < 1) return true;
    
    static float lastTime = 0.0f;
    float currentTime = SDL_GetTicks() * 0.001f;
    if((currentTime - lastTime) > (1.0f / framerate))
    {
        lastTime = (currentTime - lastTime) - (1.0 / framerate);
        return true;
    }
    return false;
}

int main(int argc, char *argv[]) {
<<<<<<< local
    Prism::Graphics::Director::instance(); // Force director to be created
=======
	#if PRISM_OS_WIN
	//_CRT_INIT();
	#endif

    Prism::Director::instance(); // Force director to be created
>>>>>>> other
    
    Prism::File::native_setArgcArgv(argc, argv);
    
    prism_initialize();
    
    bool quit = false;
    SDL_Event event;
    while(quit == false)
	{
		if(SDL_PollEvent(&event))
		{
			//If a key was pressed
			if( event.type == SDL_KEYDOWN )
			{
				//Adjust the velocity
				switch( event.key.keysym.sym )
				{
                    case SDLK_ESCAPE:
                        quit=true;
                        break;
				}
			}
            
            if (event.type == SDL_VIDEORESIZE) {
                Prism::Size newSize;
                newSize.width = event.resize.w;
                newSize.height = event.resize.h;
                Prism::Graphics::Director::instance()->setWinSize(newSize);
            }
		}
        
        if (LockFramesPerSecond(Prism::Graphics::theDirector()->maxFrameRate()))
            Prism::Graphics::theDirector()->drawScene();
	}
    
    prism_terminate();

	return 1;
}

int main2() {
    //The images
    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;
    
    //Start SDL
    //SDL_Init( SDL_INIT_EVERYTHING );
    if( SDL_Init(SDL_INIT_VIDEO) <0 )
    {
        printf("Unable to init SDL: %s\n", SDL_GetError());
        return 1;
    }
    
    //Set up screen
    screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set 640x480 hardware video: %s\n", SDL_GetError());
        screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    }
    
    Slock(screen);
    for(int x=0;x<640;x++)
    {
        for(int y=0;y<480;y++)
        {
            DrawPixel(screen, x,y,y/2,y/2,x/3);
        }
    }
    Sulock(screen);
    SDL_Flip(screen);
    
    //Load image
    hello = IMG_Load("preview2.jpg");
    
    //Apply image to screen
    SDL_BlitSurface( hello, NULL, screen, NULL );
    
    //Update Screen
    SDL_Flip( screen );
    
    //Pause
    SDL_Delay( 2000 );
    
    //Free the loaded image
    SDL_FreeSurface( hello );
    
    //Quit SDL
    SDL_Quit();
    
    return 0;
}

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
    Uint32 color = SDL_MapRGB(screen->format, R, G, B);
    switch (screen->format->BytesPerPixel)
    {
        case 1: // Assuming 8-bpp
        {
            Uint8 *bufp;
            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
            *bufp = color;
        }
            break;
        case 2: // Probably 15-bpp or 16-bpp
        {
            Uint16 *bufp;
            bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
            *bufp = color;
        }
            break;
        case 3: // Slow 24-bpp mode, usually not used
        {
            Uint8 *bufp;
            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
            if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
            {
                bufp[0] = color;
                bufp[1] = color >> 8;
                bufp[2] = color >> 16;
            } else {
                bufp[2] = color;
                bufp[1] = color >> 8;
                bufp[0] = color >> 16;
            }
        }
            break;
        case 4: // Probably 32-bpp
        {
            Uint32 *bufp;
            bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
            *bufp = color;
        }
            break;
    }
}

void Slock(SDL_Surface *screen)
{
    if ( SDL_MUSTLOCK(screen) )
    {
        if ( SDL_LockSurface(screen) < 0 )
        {
            return;
        }
    }
}

void Sulock(SDL_Surface *screen)
{
    if ( SDL_MUSTLOCK(screen) )
    {
        SDL_UnlockSurface(screen);
    }
}

#endif

